import { Message } from 'element-ui';
import { ORDER } from "@/const/orderConst.js";
var ws;
export function connects(){
  StartIC();
  StartICWork();
}

var istrue = false;
function InfoCallback(op) {
	var text = "";
    if(op == 0x19){
		text = "身份证功能启动成功\r\n";
	}else if(op == 0x1a){
		text = "身份证功能启动失败\r\n";
	}else if(op == 0x1b){
        text = "身份证读卡成功\r\n";
        Message.success("身份证读卡成功");
	}else if(op == 0x1c){
		text = "身份证读卡失败\r\n";
	}else if(op == 0x1d){
        text = "重新操作\r\n";
        Message.error("没有身份证。");
	}else if(op == 0x1e){
		text = "未发现模块\r\n";
	}else if(op == 0x1f){
		text = "未启动身份证功能\r\n";
	}else if(op == 0xa3){
		text = "意外断开\r\n";     
	}else if(op == 0xa4){
		text = "连接不上\r\n";     
	}else if(op == 0x20){
		text = "启动身份证自动读卡\r\n";
	}else if(op == 0x21){
		text = "关闭身份证自动读卡\r\n";
    }
    console.log(text);
  return text;
}

// function isIE() { //ie?
//     if (!!window.ActiveXObject || "ActiveXObject" in window)
//      return true;
//      else
//      return false;
// }
//开启身份证模块
function StartIC(){
    sendMsgStartIDCard();
}
function sendMsgStartIDCard(){
    var aDataArray = new Uint8Array(4);
    aDataArray[0] = 0xef;
    aDataArray[1] = 0x3c;
    aDataArray[2] = 0x01;
    aDataArray[3] = 0x00;
    ws.send(aDataArray.buffer);
}


// ws=new WebSocket("ws://"+"localhost"+":9002"); 
// ws.binaryType = "arraybuffer" ;
var text;

//  ws.onmessage = function(event) { 
//           var aDataArray = new Uint8Array( event.data );
//           if(aDataArray.length>0){
//               if(aDataArray[0] ==0xef && aDataArray[1] ==0x23){
//                   var type = aDataArray[3];
//                   var len = aDataArray[4];
//                   var data = new Uint8Array(len);
//                   for(var i=0;i<len;i++){
//                     data[i] = aDataArray[5+i];
//                   }
//                   var str = byteToString(data)
//                   var text = decodeURIComponent(str);
//                   InfoTextCallback(type,text);
// 			    }else if (aDataArray[0] ==0xef && aDataArray[1] ==0x14){
//                     var tmp = aDataArray[3];
//                     InfoCallback(tmp);
//               }
//             }

// 	}; 


    // }
// }          

// ws.onclose = function(event) { //实例对象的onclose属性，用于指定连接关闭后的回调函数。
//     if(ORDER.Highmeter == 20){
//         InfoCallback(0xa3);
//         alert("程序出现异常,请重新启动0x00"); 
//     }
    
// }; 
// ws.onopen = function(event) { //实例对象的onopen属性，用于指定连接成功后的回调函数。
//   InfoCallback(0x24);
// }; 
// ws.onerror =function(event){//实例对象的onerror属性，用于指定报错时的回调函数。
//     if(ORDER.Highmeter == 20){
//         InfoCallback(0xa4);
//         alert("程序出现异常,请重新启动0x01"); 
//     }
// };
function byteToString(arr) {  
    if(typeof arr === 'string') {  
        return arr;  
    }  
    var str = '',  
        _arr = arr;  
    for(var i = 0; i < _arr.length; i++) {  
        var one = _arr[i].toString(2),  
            v = one.match(/^1+?(?=0)/);  
        if(v && one.length == 8) {  
            var bytesLength = v[0].length;  
            var store = _arr[i].toString(2).slice(7 - bytesLength);  
            for(var st = 1; st < bytesLength; st++) {  
                store += _arr[st + i].toString(2).slice(2);  
            }  
            str += String.fromCharCode(parseInt(store, 2));  
            i += bytesLength - 1;  
        } else {  
            str += String.fromCharCode(_arr[i]);  
        }  
    }  
    return str;  
}  
var ssss;
function leuh(){
    ssss = 21212;
}
        

function sendMsgGetICValues(type){
    var aDataArray = new Uint8Array(4);
    aDataArray[0] = 0xef;
    aDataArray[1] = 0x3d;
    aDataArray[2] = 0x01;
    aDataArray[3] = type;
    if (ws.readyState===1) {
        ws.send(aDataArray.buffer);
    }else{
       
    }
}

//自动读卡
function StartICWork(){
          sendMsgWorkIDCard();
}

function sendMsgWorkIDCard(){
    var aDataArray = new Uint8Array(4);
    aDataArray[0] = 0xef;
    aDataArray[1] = 0x41;
    aDataArray[2] = 0x01;
    aDataArray[3] = 0x01;
    ws.send(aDataArray.buffer);
}



//手动读卡
function GetOneIC(){
           sendMsgGetOneIC();
}

function sendMsgGetOneIC(){
    var aDataArray = new Uint8Array(4);
    aDataArray[0] = 0xef;
    aDataArray[1] = 0x3c;
    aDataArray[2] = 0x01;
    aDataArray[3] = 0x01;
    ws.send(aDataArray.buffer);
}

//获取身份证信息
function GetICValues(){
        for(var i=0;i<6;i++){
            sendMsgGetICValues(i);
        }
}

  var text1 = '';
  var text2 = '';
  var text3 = '';
  var text4 = '';
  var text5 = '';
  var text6 = '';  
 const textnum = {};
     
function InfoTextCallback(type,str){
if(type == 9){
    text1 = str;
    textnum.text1 = str;
}else if(type == 10){
    text2 = str;
    textnum.text2 = str;
}else if(type == 11){
    text3 = str;
    textnum.text3 = str;
}else if(type == 12){
    text4 = str;
    textnum.text4 = str;
}else if(type == 13){
    text5 = str;
    textnum.text5 = str;
}else if(type == 14){
    text6 = str;
    textnum.text6 = str;
}
    // textnum = {
    //     text1:text1,
    //     text2:text2,
    //     text3:text1,
    //     text1:text1,
    //     text1:text1,
    //     text1:text1,
    // }
    console.log(textnum);
}

export{textnum}








